﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Mst;
using ESOL.Business.Hr;
using ESOL.Common;
using ESOL.Common.Entity;
using ESOL.WinUI.MasterData;
using ESOL.WinUI.UICommon;

namespace ESOL.WinUI.Hr
{
    public partial class frmMaintainCardLossApplication : BaseForm.frmBaseDialogyBusinessForm
    {
        #region Variable
        private MstPersonEntity mSelectedPerson;
        private HrAttendanceCardLossEntity mSelectedCardLoss;
        #endregion

        #region Constructor
        public frmMaintainCardLossApplication(HrAttendanceCardLossEntity entity)
        {
            InitializeComponent();
            mSelectedCardLoss = entity;
        }
        public frmMaintainCardLossApplication(MstPersonEntity person)
        {
            InitializeComponent();
            mSelectedPerson = person;
        }
        public frmMaintainCardLossApplication()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private void SetPersonFields()
        {
            txtPerson.Text = "";
            txtPersonNameCn.Text = "";
            cmbStatus.SelectedIndex = -1;
            txtMobile.Text = "";
            txtDepartment.Text = "";
            if (mSelectedPerson == null)
            {
                return;
            }
            txtPerson.Text = mSelectedPerson.PersonCode;
            txtPersonNameCn.Text = mSelectedPerson.NameCn;
            cmbStatus.SetSelectedValue(mSelectedPerson.Status);
            txtMobile.Text = mSelectedPerson.Mobile;
            MstDepartmentEntity department = MstDepartmentBs.GetMstDepartmentByID(mSelectedPerson.DepartmentId);
            if (department != null)
            {
                txtDepartment.Text = department.DepartmentName;
            }
        }

        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "STATUS", false);
        }

        private void SetFields()
        {
            if (mSelectedCardLoss == null)
            {
                return;
            }
            if (string.IsNullOrEmpty(mSelectedCardLoss.PersonId) == false)
            {
                mSelectedPerson = MstPersonBs.GetMstPersonByID(mSelectedCardLoss.PersonId);
                SetPersonFields();
            }
            if (mSelectedCardLoss.CardLossDate.ToString("yyyy-MM-dd") != "1900-01-01" &&
                 mSelectedCardLoss.CardLossDate.ToString("yyyy-MM-dd") != "0001-01-01")
            {
                dtpCardLossDate.EditValue = mSelectedCardLoss.CardLossDate;
            }
            cmbStatus.SetSelectedValue(mSelectedCardLoss.Status);
            txtReason.Text = mSelectedCardLoss.Reason;
            txtApprovedRemark.Text = mSelectedCardLoss.ApprovedRemark;

            UICommonFunctions.SetTextBoxDate(txtCreatedDate, mSelectedCardLoss.CreatedDate, "yyyy-MM-dd hh:mm:ss");
            UICommonFunctions.SetTextBoxPersonName(txtCreatedBy, mSelectedCardLoss.CreatedBy);
            UICommonFunctions.SetTextBoxDate(txtLastModifiedDate, mSelectedCardLoss.LastModifiedDate, "yyyy-MM-dd hh:mm:ss");
            UICommonFunctions.SetTextBoxPersonName(txtLastModifiedBy, mSelectedCardLoss.LastModifiedBy);
            UICommonFunctions.SetTextBoxDate(txtApprovedDate, mSelectedCardLoss.ApprovedDate, "yyyy-MM-dd hh:mm:ss");
            UICommonFunctions.SetTextBoxPersonName(txtApprovedBy, mSelectedCardLoss.ApprovedBy);
        }

        private void SetEntity()
        {
            if (mSelectedCardLoss == null)
            {
                mSelectedCardLoss = new HrAttendanceCardLossEntity();
                mSelectedCardLoss.LossId = Guid.NewGuid().ToString();
            }
            if (mSelectedPerson != null)
            {
                mSelectedCardLoss.PersonId = mSelectedPerson.PersonId;
                mSelectedCardLoss.DepartmentId = mSelectedPerson.DepartmentId;
            }
            if (dtpCardLossDate.EditValue!=null)
            {
                mSelectedCardLoss.CardLossDate = Convert.ToDateTime(dtpCardLossDate.EditValue);
            }
            mSelectedCardLoss.Status = "STATUS02";
            mSelectedCardLoss.Reason = txtReason.Text;
            //mSelectedCardLoss.ApprovedRemark = txtApprovedRemark.Text;

            if (ThisActionType == ActionType.New)
            {
                mSelectedCardLoss.CreatedDate = DateTime.Now;
                mSelectedCardLoss.CreatedBy = CommonParameter.CurrentUser.PersonId;
            }
            if (ThisActionType == ActionType.New || ThisActionType == ActionType.Modify)
            {
                mSelectedCardLoss.LastModifiedDate = DateTime.Now;
                mSelectedCardLoss.LastModifiedBy = CommonParameter.CurrentUser.PersonId;
            }
        }

        private bool CheckData()
        {
            if (txtPerson .Text  == null)
            {
                MessageBox.Show("请选择一个员工！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (dtpCardLossDate.EditValue!=null)
            {
                MessageBox.Show("补卡时间不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(txtReason.Text))
            {
                MessageBox.Show("补卡原因不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            return true;
        }

        private bool Save()
        {
            try
            {
                if (CheckData() == false)
                {
                    return false;
                }
                if (ThisActionType == ActionType.New)
                {
                    SetEntity();
                    bool isSuccessful = HrAttendanceCardLossBs.InsertHrAttendanceCardLoss(mSelectedCardLoss);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("新增失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                else if (ThisActionType == ActionType.Modify)
                {
                    SetEntity();
                    bool isSuccessful = HrAttendanceCardLossBs.UpdateHrAttendanceCardLoss(mSelectedCardLoss);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("修改失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                else if (ThisActionType == ActionType.Approve)
                {
                    mSelectedCardLoss.ApprovedDate = DateTime.Now;
                    mSelectedCardLoss.ApprovedBy = CommonParameter.CurrentUser.PersonId;
                    bool isSuccessful = HrAttendanceCardLossBs.UpdateHrAttendanceCardLoss(mSelectedCardLoss);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("审核失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }
        #endregion

        #region Event
        protected override void OnShown(EventArgs e)
        {
            BindComboBox();
            SetPersonFields();
            SetFields();
        }

        protected override void Click_Save()
        {
            if (Save())
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        protected override void Click_Approve()
        {
            if (mSelectedCardLoss == null)
            {
                return;
            }
            mSelectedCardLoss.Status = "STATUS03";
            mSelectedCardLoss.ApprovedRemark = txtApprovedRemark.Text;
            if (Save())
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        protected override void Click_Reject()
        {
            if (mSelectedCardLoss == null)
            {
                return;
            }
            mSelectedCardLoss.Status = "STATUS04";
            mSelectedCardLoss.ApprovedRemark = txtApprovedRemark.Text;
            if (Save())
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        private void txtPerson_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F2)
            {
                frmMaintainEmployeeQuery frm = new frmMaintainEmployeeQuery(true);
                if (frm.ShowDialog() == DialogResult.OK && frm.SelectedPerson != null)
                {
                    mSelectedPerson = frm.SelectedPerson;
                    SetPersonFields();
                }
                else
                {
                    mSelectedPerson = null;
                    SetPersonFields();
                }
            }
        }
        #endregion

        private void txtPerson_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainEmployeeQuery frm = new frmMaintainEmployeeQuery(true);
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedPerson != null)
            {
                txtPerson.Tag = frm.SelectedPerson;
                txtPerson.Text = frm.SelectedPerson.PersonCode;//*
            }
            else
            {
                txtPerson.Tag = null;
                txtPerson.Text = "";
            }
        }
    }
}
